package com.probiz.estoresa.product.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.BrandManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.ProductTypeManager;

public class ProductSelectorController extends
		GenericStoreAdminController<Product> {

	private ProductManager productManager = null;
	private BrandManager brandManager = null;
	private ProductTypeManager productTypeManager = null;
	private ProductCategoryManager productCategoryManager = null;


	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	
	public void setProductCategoryManager(
			ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}
	

	public void setBrandManager(BrandManager brandManager) {
		this.brandManager = brandManager;
	}

	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Product entity) {
		return entity.getProductName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = productManager;
	}

	

	@Override
	protected void preSave(HttpServletRequest request, Product entity,
			BindException errors) {
		
	}

	
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("product/productSelector");
		ProductCategory treeRoot = productCategoryManager.getById(1);
		Set<ProductCategory> categorySet = treeRoot.getChildren();
		List<ProductCategory> categoryList = new ArrayList<ProductCategory>();
		categoryList.addAll(categorySet);
		mv.addObject("categoryList", categoryList);
		mv.addObject("treeRoot", treeRoot);
		mv.addObject("productTypeList",productTypeManager.listAll());
		mv.addObject("brandList", brandManager.listAll());
		return mv;
	}
	
	
	
	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("product/productSelectorResults");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria();
		try {
			// 绑定参数
			bind(request, productSearchCriteria);
			List<Product> productList = productManager.searchByCriteria(productSearchCriteria, searchCriteria);
			mv.addObject("productList",productList);
			mv.addObject("productSearchCriteria",productSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return mv;
		
		
	}
	
}
